package com.gress.shop.service.mail.impl;

import com.gress.shop.bo.Email;
import com.gress.shop.config.BaseConfig;
import com.gress.shop.constants.EmailConstants;
import com.gress.shop.service.IEmailService;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;

/**
 * 网易邮箱发送服务
 */
@Service("NTESMailServiceImpl")
public class NTESMailServiceImpl extends AbstractMailService {
    @Resource
    private JavaMailSenderImpl mailSender;
    @Resource
    private BaseConfig baseConfig;
    @Resource
    private VelocityEngine velocityEngine;
    @Resource
    private IEmailService emailService;

    @Override
    public SwalReturn send(String to, String subject, String text) {
        String mailFrom = baseConfig.getMailFrom();
        // 记录发送邮件
        Email email = new Email();
        email.setTo(to);
        email.setSubject(subject);
        email.setText(text);
        email.setOpOne(EmailConstants.Type.SUCCESS);
        email.setOpTwo(EmailConstants.Type.SYSTEM);
        email.setFrom(mailFrom);

        SimpleMailMessage mail = new SimpleMailMessage();
        //收件人邮箱地址
        mail.setTo(to);
        //收件人
        mail.setFrom(mailFrom);
        //主题
        mail.setSubject(subject);
        //正文
        mail.setText(text);
        try {
            mailSender.send(mail);
        } catch (MailException e) {
            e.printStackTrace();
            email.setOpOne(EmailConstants.Type.ERROR);
            return SwalFactory.getErrorSwal("系统异常");
        }finally {
            emailService.insertSelective(email);
        }
        return SwalFactory.getSuccessSwal();
    }


    @Override
    public SwalReturn sendTemplateText(String to, String templateName, String subject, Map model) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to); //接收人
        simpleMailMessage.setFrom(baseConfig.getMailFrom()); //发送人,从配置文件中取得
        simpleMailMessage.setSubject(subject);
        String result = null;
        try {
            result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8", model);
            // 这里没有拿到文本信息就直接返回false
            if (StringUtils.isEmpty(result)) {
                return SwalFactory.getErrorSwal();
            }
            simpleMailMessage.setText(result);
            mailSender.send(simpleMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return SwalFactory.getErrorSwal();
        }
        return SwalFactory.getSuccessSwal();
    }

    @Override
    public SwalReturn sendTemplateHtml(String to, String templateName, String subject, Map model) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
        String result = null;
        try {
            result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8",model);
            // 这里没有拿到文本信息就直接返回false
            if (StringUtils.isEmpty(result)) {
                return SwalFactory.getErrorSwal();
            }
        } catch (Exception e) {
            return SwalFactory.getErrorSwal();
        }

        try {
            messageHelper.setTo(to);
            messageHelper.setFrom(baseConfig.getMailFrom());
            messageHelper.setSubject(subject);
            messageHelper.setText(result,true);
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return SwalFactory.getErrorSwal();
        }
        return SwalFactory.getSuccessSwal();
    }

}
